﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Physis.Core.Insurer {

    public class Rider  : CoreConfigurationObject {

        #region Private Properties

        private Int64 insuranceTypeId = 0;

        private String uniqueId = String.Empty;

        private String legacyId = String.Empty;


        private Program program = null;

        private InsuranceType insuranceType = null;

        #endregion 


        #region Public Properties

        public Int64 InsuranceTypeId { get { return insuranceTypeId; } set { insuranceTypeId = value; } }

        public InsuranceType InsuranceType {

            get {

                if (insuranceType == null) { insuranceType = Application.Core.Insurer.InsuranceType (insuranceTypeId, true); }

                return insuranceType;

            }

        }

        public String InsuranceTypeName { get { return ((InsuranceType != null) ? InsuranceType.Name : String.Empty); } }
        

        public String UniqueId { get { return uniqueId; } set { uniqueId = Data.Functions.SetValueMaxLength (value, Data.DataTypeConstants.BusinessId); } }

        public String LegacyId { get { return legacyId; } set { legacyId = Data.Functions.SetValueMaxLength (value, Data.DataTypeConstants.BusinessId); } }

        #endregion 

        
        #region Constructors

        public Rider (Application forApplication) : base (forApplication) {

            return;

        }

        public Rider (Application forApplication, Int64 forId) : base (forApplication, forId) {

            return;

        }

        public Rider (Application forApplication, String forName) : base (forApplication, forName) {

            return;

        }

        #endregion


        #region Database Functions

        protected override Dictionary<String, String> Validate () {

            Dictionary<String, String> validationErrors = base.Validate ();


            if (InsuranceType == null) { validationErrors.Add ("Insurance Type", "In appropriate or missing insurance type."); }


            return validationErrors;

        }

        public override void MapDataFields (System.Data.DataRow currentRow) {
            
            base.MapDataFields (currentRow);
            
            InsuranceTypeId = Data.Readers.DataReader.Int64FromDatabase (currentRow, "InsuranceTypeId");
            
            UniqueId = Data.Readers.DataReader.StringFromDatabase (currentRow, "UniqueId");

            LegacyId = Data.Readers.DataReader.StringFromDatabase (currentRow, "LegacyId");

            return;

        }

        public override Boolean Save () {

            Boolean success = false;

            try {

                Dictionary<String, String> validationErrors = Validate ();

                if (validationErrors.Count > 0) {

                    Application.SetLastException (new Exception ("Invalid [" + validationErrors.Keys.First () + "]: " + validationErrors.Values.First ()));

                    return false;

                }

                UpdateModifiedToSessionUser ();


                System.Data.IDbCommand command = Application.EnvironmentDatabase.CreateCommand (ObjectType + "_InsertUpdate");

                command.CommandType = System.Data.CommandType.StoredProcedure;


                Application.EnvironmentDatabase.AppendCommandParameter (command, "Id", Id);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "Name", Name, 60);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "Description", Description, 999);
                
                Application.EnvironmentDatabase.AppendCommandParameter (command, "InsuranceTypeId", InsuranceTypeId);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "UniqueId", ((String.IsNullOrWhiteSpace (UniqueId)) ? null : UniqueId), 20); // ALLOW THIS TO PASS AS A NULL

                Application.EnvironmentDatabase.AppendCommandParameter (command, "LegacyId", LegacyId, 20);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "Enabled", Enabled);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "ModifiedId", Application.Session.UserAccountName, 60);

                success = (command.ExecuteNonQuery () > 0);

                if (success) { SetIdentity (); }

            }

            catch (Exception exception) {

                success = false;

                Application.SetLastException (exception);

            }

            return success;

        }

        public override Boolean Delete () {

            return base.DeleteFromDatabase (DeleteStatementById (), Application.EnterpriseDatabase);

        }

        #endregion 


    }

}
